package application.manager;

import application.anno.AutoClose;
import application.anno.Injection;
import application.manager.annotationManager.AutoCloseManager;
import application.parasitic.ParasiticLayout;

import java.util.HashMap;
import java.util.Map;

@Injection(staticMethodName = "getInstance")
@AutoClose(methodName = "release")
public class ParasiticManager {

    private static ParasiticManager parasiticManager = new ParasiticManager();

    private Map<Class<? extends ParasiticLayout>, ParasiticLayout> parasiticLayouts;

    private ParasiticManager() {
        AutoCloseManager.getInstance().getAutoCloses().put(this.getClass(), this);
        parasiticLayouts = new HashMap<>();
    }

    public static ParasiticManager getInstance() {
        if (parasiticManager == null) {
            parasiticManager = new ParasiticManager();
        }
        return parasiticManager;
    }

    public ParasiticLayout register(ParasiticLayout parasiticLayout) {
        this.parasiticLayouts.put(parasiticLayout.getClass(), parasiticLayout);
        return parasiticLayout;
    }

    public ParasiticLayout getParasiticLayout(Class<? extends ParasiticLayout> cls) {
        return this.parasiticLayouts.get(cls);
    }

    public void unregister(Class<? extends ParasiticLayout> cls) {
        this.parasiticLayouts.remove(cls);
    }

    public void release() {
        for (Map.Entry<Class<? extends ParasiticLayout>, ParasiticLayout> entry : this.parasiticLayouts.entrySet()) {
            ParasiticLayout value = entry.getValue();
            value.release();
        }
        this.parasiticLayouts.clear();
    }
}
